
library(XML)

sourceUrl <- "http://api.worldbank.org/sources/"
topicUrl <- "http://api.worldbank.org/topics/"

sourceDoc = xmlParse(sourceUrl)


topicDoc = xmlParse(topicUrl)

doc = xmlParse("http://localhost/tmp/DPANUSSPF1.xml")

lapply(getNodeSet(doc, "//wb:data/wb:country[@id]"), xmlAttrs)

countryNames <- unique(lapply(getNodeSet(doc, "//wb:data/wb:country[@id]"), xmlAttrs))

xmlPathName <- function(cName) {
  xmlPathName <- paste("//wb:data/wb:country[@id=", cName, "]",sep="'")
  return (xmlPathName)
}

convertData <- function(xmlPathName) {
  dtFrame <- xmlToDataFrame( nodes = sapply(getNodeSet(doc, xmlPathName), xmlParent) )
  # convertedDt <- sapply(getNodeSet(doc, xmlPathName), xmlParent)
  return (dtFrame)
}

wbDt <- lapply(lapply(countryNames, xmlPathName), convertData)

as.numeric(as.character(wbDt[[1]]$value))

wbLm <- lm( as.numeric(as.character(wbDt[[1]]$value)) ~ as.numeric(as.character(wbDt[[2]]$value)) )

summary(wbLm)

sapply(getNodeSet(doc, "//wb:data/wb:country[@id = 'IN']"), xmlParent)

sapply(sapply(getNodeSet(doc, "//wb:data/wb:country[@id = 'IN']"), xmlParent), xmlName)

sapply(sapply(getNodeSet(doc, "//wb:data/wb:country[@id = 'IN']"), xmlParent), xmlSize)

sapply(sapply(getNodeSet(doc, "//wb:data/wb:country[@id = 'IN']"), xmlParent), xmlChildren)

